#include "particlemanagement.h"

ParticleManagement::ParticleManagement()
{
    vecParticles = new QVector<Particle*>();

}

ParticleManagement::ParticleManagement(int n)
{
    this->vecParticles = new QVector<Particle*>();

    for(int i = 0; i<n; ++i)
    {
        addParticle(ParticleManagement::generateRandomParticle());
    }


}

ParticleManagement::~ParticleManagement()
{
    Particle* p;
    for(int i = 0; i<vecParticles->size(); ++i)
    {
        delete vecParticles->at(i);
    }
    delete vecParticles;
}

QVector3D ParticleManagement::getPos(int pos)
{
    return vecParticles->at(pos)->getPos();
}
int ParticleManagement::resize(int size)
{


    if(vecParticles->size()>size)
    {
            while(vecParticles->size()>size)
             {
              vecParticles->remove(vecParticles->size()-1);
             }
    }
    if(vecParticles->size()<size)
    {
            while(vecParticles->size()<size)
            {
                addParticle(ParticleManagement::generateRandomParticle());
            }
    }
    return vecParticles->size();
}

int ParticleManagement::addParticle(Particle *p)
{
    vecParticles->append(p);
}

 Particle* ParticleManagement::createParticle(QVector3D pos, QVector3D dir)
{
    return new Particle(pos,dir);
}

 QVector3D ParticleManagement::generateRandomQVector3D()
{
  QVector3D v(((qreal)(rand() % SYSTEM_DIM_X +1 -SYSTEM_DIM_X/2)),
                        ((qreal)(rand() % SYSTEM_DIM_Y +1 -SYSTEM_DIM_Y/2)),
                        ((qreal)(rand() % SYSTEM_DIM_Z +1 -SYSTEM_DIM_Z/2)));
  return v;
}

 Particle* ParticleManagement::generateRandomParticle()
{
    return ParticleManagement::createParticle(ParticleManagement::generateRandomQVector3D(),
                        ParticleManagement::generateRandomQVector3D());
}

 int ParticleManagement::fillWithParticles(int n)
 {
     resize(n);
 }

 void ParticleManagement::moveParticlesInTime(double t)
 {
     for(int i = 0; i<vecParticles->size();++i)
     {
         vecParticles->at(i)->moveInTime(t);
     }
 }
